package com.dao;
import com.bean.Student;
import com.utils.JDBCUtils;
import java.sql.*;
import java.text.SimpleDateFormat;
import java.util.HashMap;
public class SrudentDao implements StudentDaoIf {
    //登陆用户
    @Override
    public HashMap<String, String> login(){
        HashMap<String, String> hm = new HashMap<>();
        try {
            Connection con = JDBCUtils.getConnection();
            Statement statement = con.createStatement();
            ResultSet rs = statement.executeQuery("SELECT * FROM user");
            while (rs.next()){
                String username1 = rs.getString("username");
                String password = rs.getString("password");
                hm.put(username1,password);
            }
            JDBCUtils.close(rs,statement,con);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return hm;
    }
    //注册用户
    @Override
    public int newUser(String username, String psw) {
        int rs = 0;
        try {
            Connection con = JDBCUtils.getConnection();
            Statement statement = con.createStatement();
            rs = statement.executeUpdate("INSERT INTO user VALUES('"+username+"','"+psw +"')");
            JDBCUtils.close(statement,con);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return rs;
    }
    @Override
    public HashMap<String, Student> findStudent() {
        HashMap<String, Student> hm = new HashMap<>();
        try {
            Connection con = JDBCUtils.getConnection();
            Statement statement = con.createStatement();
            ResultSet rs = statement.executeQuery("SELECT * FROM student");
            while (rs.next()){
                String stuid = rs.getString("sid");
                String stuname = rs.getString("name");
                int stuage = rs.getInt("age");
                Date birthday = rs.getDate("birthday");
                hm.put(stuid,new Student(stuid,stuname,stuage,birthday));
                System.out.println(stuid+stuname+stuage+birthday);
            }
            JDBCUtils.close(rs,statement,con);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return hm;
    }
    @Override
    public int delete(String id) {
        int i = 0;
        try {
            Connection con = JDBCUtils.getConnection();
            Statement statement = con.createStatement(); //学会了PreparedStatement, 就不要再用Statement了, 他的本质就是继承了Statement,所以他是Statement的儿子
            i = statement.executeUpdate("DELETE FROM student WHERE sid="+id);  //如果数据库中为int类型的时候就不需要拼接
            JDBCUtils.close(statement,con);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return i;
    }
    @Override
    public int update(Student student) {
        PreparedStatement st=null;
        int i=0;
        try {
            Connection con = JDBCUtils.getConnection();

            SimpleDateFormat sim = new SimpleDateFormat("yyyy-MM-dd");
            String sql="UPDATE student SET name=?,age=?,birthday=? WHERE sid=?";
            String birthday = sim.format(student.getBirthday());
            st = con.prepareStatement(sql);
//            st.setString(1,student.getId());

            st.setString(1,student.getName());
            st.setInt(2,student.getAge());
            st.setString(3,birthday);
            st.setString(4,student.getId());

            i=st.executeUpdate();
            JDBCUtils.close(st,con);

    } catch (SQLException e) {
        e.printStackTrace();
    }
        return i;
    }


}
